package com.yiren.backstage.judge.services.light.impl;

import android.content.Context;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.VoiceBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.enums.CarStateValueEnum;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.judge.handler.LightJudgeHandler;
import com.yiren.backstage.judge.services.light.ILightStartTipsService;
import com.yiren.backstage.player.api.IPlayerCallBack;
import com.yiren.backstage.player.api.PlayerApi;

/**
 * Time:2022/3/25 9:02
 * Author:xiaozan
 * Description:灯光操作前提示
 */
public class LightStartTipsServiceImpl implements ILightStartTipsService {

    private Context context;
    private LightJudgeHandler lightJudgeHandler;
    private CarStateBean carState;

    /**
     * 是否正在播报
     */
    private boolean isPlay;

    public LightStartTipsServiceImpl(Context context, LightJudgeHandler lightJudgeHandler) {
        this.context = context;
        this.lightJudgeHandler = lightJudgeHandler;
        carState = CarStateBean.getInstance();
    }

    @Override
    public boolean tipsPlayStartEngine() {
        try {
            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D1.toString())) {
                if (carState.getRotateSpeed() < 200) {
                    String notStartEngineTipsContent = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D2.toString());
                    if ("".equals(notStartEngineTipsContent)) return true;
                    isPlay = true;
                    PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(notStartEngineTipsContent, ""), new IPlayerCallBack() {
                        @Override
                        public void playFinish() {
                            isPlay = false;
                        }
                    });
                    //等待播报完成
                    while (isPlay && lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                        Thread.sleep(100);
                    }
                    //休眠间隔时间
                    Thread.sleep((int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D3.toString()) * 1000);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean tipsPlayCloseLight() {
        try {
            //灯光开启就提示关闭灯光
            if ((boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D4.toString())) {
                boolean closeAllLight = carState.getDrivingLamp() == CarStateValueEnum.CLOSE && carState.getDistanceLight() == CarStateValueEnum.CLOSE
                        && carState.getLeftLight() == CarStateValueEnum.CLOSE && carState.getRightLight() == CarStateValueEnum.CLOSE
                        && carState.getFoglight() == CarStateValueEnum.CLOSE;
                if (!closeAllLight) {
                    String notCloseLightTipsContent = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D5.toString());
                    if ("".equals(notCloseLightTipsContent)) return true;
                    isPlay = true;
                    PlayerApi.playProjectAndJudgeVoice(context, new VoiceBean(notCloseLightTipsContent, ""), new IPlayerCallBack() {
                        @Override
                        public void playFinish() {
                            isPlay = false;
                        }
                    });
                    //等待播报完成
                    while (isPlay && lightJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
                        Thread.sleep(100);
                    }
                    //休眠间隔时间
                    Thread.sleep((int) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.D6.toString()) * 1000);
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}